#include "../include/TcpServer.h"

void TcpServer::NewConnection(int connfd)
{
    if (connfd < 0)
        return;
    // DBG_LOG("New connection accepted, fd=%d", connfd);
    PtrConnection new_coon = std::make_shared<Connection>(_threads_pool.getNextLoop(), _nextid, connfd);
    new_coon->SetConnectedCallBack(_conn_cb);
    new_coon->SetClosedCallBack(_close_cb);
    new_coon->SetMessageCallBack(_mess_cb);
    new_coon->SetRandomEventCallBack(_rand_ev_cb);
    new_coon->SetServerClosedCallBack(std::bind(&TcpServer::RemoveConnection, this, std::placeholders::_1));

    if (_enable_inactive_release)
        new_coon->EnableInactiveRelease(_timeout);
    new_coon->Established();
    _conn.emplace(_nextid, new_coon);
    _nextid++;
}

void TcpServer::RemoveConnection(const PtrConnection &conn) { _baseloop->RunInLoop(std::bind(&TcpServer::RemoveConnectionInLoop, this, conn)); }

void TcpServer::RemoveConnectionInLoop(const PtrConnection &conn)
{
    int id = conn->getId();
    if (_conn.find(id) != _conn.end())
        _conn.erase(id);
}

void TcpServer::RunAfterInLoop(const Functor &func, uint32_t delay)
{
    // 添加一个定时任务，保证线程安全
    _baseloop->TimerAdd(_nextid, delay, func);
    _nextid++;
}

// TcpServer::TcpServer(EventLoop *loop, uint16_t port)
//     : _port(port),
//       _nextid(0),
//       _baseloop(loop),
//       _own_loop(false),
//       _enable_inactive_release(false),
//       _acceptor(_baseloop, port),
//       _threads_pool(_baseloop)
// {
//     _acceptor.SetAcceptorCallBack(std::bind(&TcpServer::NewConnection, this, std::placeholders::_1));
//     _acceptor.Listen();
// }

// 新增：自动创建 EventLoop 的构造函数
TcpServer::TcpServer(uint16_t port)
    : _port(port),
      _nextid(0),
      _baseloop(new EventLoop()),
      _own_loop(true),
      _enable_inactive_release(false),
      _acceptor(_baseloop, port),
      _threads_pool(_baseloop)
{
    _acceptor.SetAcceptorCallBack(std::bind(&TcpServer::NewConnection, this, std::placeholders::_1));
    _acceptor.Listen();
}

TcpServer::~TcpServer()
{
    // if (_own_loop && _baseloop)
    // {
    //     delete _baseloop;
    //     _baseloop = nullptr;
    // }
    delete _baseloop;
}

void TcpServer::Start()
{
    _threads_pool.Create();
    _baseloop->Start();
}

void TcpServer::setThreadCount(uint32_t threadcount) { _threads_pool.setThreadCount(threadcount); }

void TcpServer::EnableInactiveRelease(uint32_t delay)
{
    _timeout = delay;
    _enable_inactive_release = true;
}

void TcpServer::RunAfter(const Functor &func, uint32_t delay) { _baseloop->RunInLoop(std::bind(&TcpServer::RunAfterInLoop, this, func, delay)); }

void TcpServer::SetConnectedCallBack(const ConnectedCallBack &cb) { _conn_cb = cb; }

void TcpServer::SetMessageCallBack(const MessageCallBack &cb) { _mess_cb = cb; }

void TcpServer::SetRandomEventCallBack(const RandomEventCallBack &cb) { _rand_ev_cb = cb; }

void TcpServer::SetClosedCallBack(const ClosedCallBack &cb) { _close_cb = cb; }

void TcpServer::SetServerClosedCallBack(const ClosedCallBack &cb) { _close_cb = cb; }
